package com.huellas.servlet.cliente;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.huellas.excepcion.DAOExcepcion;
import com.huellas.negocio.GestionCliente;
import com.huellas.negocio.GestionRol;

/**
 * Servlet implementation class InsertarClienteServlet
 */
@WebServlet("/InsertarClienteServlet")
public class InsertarClienteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InsertarClienteServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		int CoUsuario = Integer.parseInt(request.getParameter("CoUsuario"));
		String NoUsuario = request.getParameter("NoUsuario");
		String ApPaterno = request.getParameter("ApPaterno");
		String ApMaterno = request.getParameter("ApMaterno");
		String Direccion = request.getParameter("Direccion");
		String Email = request.getParameter("Email");
		int Celular = Integer.parseInt(request.getParameter("Celular"));
		String NoRol = request.getParameter("NoRol");
		String ImagenPerfil = request.getParameter("ImagenPerfil");
		int TelefonoFijo = Integer.parseInt(request.getParameter("TelefonoFijo"));
		
		String NoTipoUsuario = "cliente";
		int CoRol = 0;
		int CoEstadoCliente = 1;
		
		try{
			GestionRol gestionRol = new GestionRol();
			CoRol = gestionRol.obtenerCoRol(NoRol);
			
			GestionCliente gestionCliente = new GestionCliente();
			
			if (gestionCliente.validarRegistroCliente(NoUsuario, ApPaterno, ApMaterno, Email, Celular, NoTipoUsuario, TelefonoFijo) == true){
				gestionCliente.insertarCliente(CoUsuario, NoUsuario, ApPaterno, ApMaterno, Direccion, Email, Celular, NoTipoUsuario, CoRol, CoEstadoCliente, ImagenPerfil, TelefonoFijo);
			}else{
				RequestDispatcher requestDispatcher = request.getRequestDispatcher("duplicado.jsp");
				requestDispatcher.forward(request, response);
			}
		}catch(DAOExcepcion e){
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("error.jsp");
			requestDispatcher.forward(request, response);
		}
	}

}
